package cn.qhuan.modules.controller;

import cn.qhuan.modules.dtos.DocumentAuthDto;
import cn.qhuan.modules.dtos.DocumentDto;
import cn.qhuan.modules.dtos.DocumentDtoVo;
import cn.qhuan.modules.dtos.DocumentShowDto;

import cn.qhuan.modules.entity.Document;
import cn.qhuan.modules.service.IDocumentService;

import cn.qhuan.modules.vo.DocumentVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.UUID;

/**
* @Description: 文档表
* @Author: 司翔宇
* @Date:   2020-12-21
* @Version: V1.0
*/
@Api(tags="文档表")
@RestController
@RequestMapping("/document")
@Slf4j
public class DocumentController extends JeecgController<Document, IDocumentService> {
   @Autowired
   private IDocumentService documentService;

    /**
     * 通过条件进行查询
     * @param documentDto
     * @return
     */
    @AutoLog(value = "文档表-通过条件分页查询")
    @ApiOperation(value="文档表-通过条件分页查询", notes="文档表-通过条件分页查询")
    @PostMapping(value = "/list")
    public Result list(@RequestBody DocumentDto documentDto){
        return documentService.findAll(documentDto);
    }
   /**
    *   添加
    *
    * @param dto
    * @return
    */
   @AutoLog(value = "文档表-添加")
   @ApiOperation(value="文档表-添加", notes="文档表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody DocumentDtoVo dto) {

       return documentService.addDocument(dto);
   }

   /**
    *  编辑
    *
    * @param dto
    * @return
    */
   @AutoLog(value = "文档表-编辑")
   @ApiOperation(value="文档表-编辑", notes="文档表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody DocumentDtoVo dto) {

       return documentService.editDocument(dto);
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "文档表-通过id删除")
   @ApiOperation(value="文档表-通过id删除", notes="文档表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       documentService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "文档表-批量删除")
   @ApiOperation(value="文档表-批量删除", notes="文档表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.documentService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询文档详情页面
    *
    * @param id
    * @return
    */
   @AutoLog(value = "文档表-通过id查询文档详情")
   @ApiOperation(value="文档表-通过id查询文档详情", notes="文档表-通过id查询文档详情")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       DocumentVO documentVO = documentService.findDocumentVo(Integer.valueOf(id));
       if(documentVO==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(documentVO);
   }



   /**
   * 导出excel
   *
   * @param request
   * @param document
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, Document document) {
       return super.exportXls(request, document, Document.class, "文档表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, Document.class);
   }

    /**
     * 审核操作
     * @param documentAuthDto
     * @return
     */
   @AutoLog(value = "文档表-审核操作")
   @ApiOperation(value = "文档表-审核操作", notes = "文档表-审核操作")
   @PostMapping("/authPass")
   public Result authPass(@RequestBody DocumentAuthDto documentAuthDto,Integer type){
       return documentService.updateStatus(documentAuthDto,type);
   }


    /**
     * 通过专题id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文档表-通过专题id查询")
    @ApiOperation(value="文档表-通过专题id查询", notes="文档表-通过专题id查询")
    @GetMapping(value = "/queryBySpcialId")
    public Result<?> queryBySpecialId(@RequestParam(name="id",required=true) String id) {


        return documentService.findDocumentIdsBySpecialId(id);

    }
    /**
     * 通过展厅id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文档表-通过展厅id查询")
    @ApiOperation(value="文档表-通过展厅id查询", notes="文档表-通过展厅id查询")
    @GetMapping(value = "/queryByExhiId")
    public Result<?> queryByExhiId(@RequestParam(name="id",required=true) String id) {
        return documentService.findDocumentIdsByExhiId(id);

    }
    /**
     * 通过类别id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文档表-通过类别id查询")
    @ApiOperation(value="文档表-通过类别id查询", notes="文档表-通过类别id查询")
    @GetMapping(value = "/queryByTypeId")
    public Result<?> queryByTypeId(@RequestParam(name="id",required=true) String id) {
        return documentService.findDocumentIdsByTypeId(id);

    }
    /**
     * 文档设置所属展厅
     * @param docId
     * @param exhiId
     * @return
     */
    @AutoLog(value = "文档表-文档设置展厅")
    @ApiOperation(value="文档表-文档设置展厅", notes="文档表-文档设置展厅")
    @GetMapping(value = "/addDocToExhi")
    public Result<?> addDocToExhi(@RequestParam(name="id",required=false) String id,String docId, String exhiId){

        return documentService.addDocToExhi(String.valueOf(UUID.randomUUID()),docId,exhiId);
    }
    /**
     * 文档设置所属专题
     * @param docId
     * @param specId
     * @return
     */
    @AutoLog(value = "文档表-文档设置专题")
    @ApiOperation(value="文档表-文档设置专题", notes="文档表-文档设置专题")
    @GetMapping(value = "/addDocToSpec")
    public Result<?> addDocToSpec(@RequestParam(name="id",required=false) String id,String docId, String specId){

        return documentService.addDocToSpec(String.valueOf(UUID.randomUUID()),docId,specId);
    }
    /**
     * 文档设置所属类别
     * @param docId
     * @param typeId
     * @return
     */
    @AutoLog(value = "文档表-文档设置类别")
    @ApiOperation(value="文档表-文档设置类别", notes="文档表-文档设置类别")
    @GetMapping(value = "/addDocToType")
    public Result<?> addDocToType(@RequestParam(name="id",required=false) String id,String docId, String typeId){

        return documentService.addDocToType(String.valueOf(UUID.randomUUID()),docId,typeId);
    }

    /**
     * 文档设置所属艺术家
     * @param docId
     * @param artistId
     * @return
     */
    @AutoLog(value = "文档表-文档设置艺术家")
    @ApiOperation(value="文档表-文档设置艺术家", notes="文档表-文档设置艺术家")
    @GetMapping(value = "/addDocToArtist")
    public Result<?> addDocToArtist(@RequestParam(name="id",required=false) String id,String docId, String artistId){

        return documentService.addDocToArtist(String.valueOf(UUID.randomUUID()),docId,artistId);
    }
    /**
     * 文档设置所属标签
     * @param docId
     * @param tagId
     * @return
     */
    @AutoLog(value = "文档表-文档设置所属标签")
    @ApiOperation(value="文档表-文档设置所属标签", notes="文档表-文档设置所属标签")
    @GetMapping(value = "/addDocToTag")
    public Result<?> addDocToTag(@RequestParam(name="id",required=false) String id,String docId, String tagId){

        return documentService.addDocToTag(String.valueOf(UUID.randomUUID()),docId,tagId);
    }
    /**
     * 前台是否显示
     * @param dto
     * @return
     */
    @AutoLog(value = "文档表-前台是否显示")
    @ApiOperation(value = "文档表-前台显示操作", notes = "文档表-前台是否显示")
    @PostMapping("/show")
    public Result showWorks(@RequestBody DocumentShowDto dto,Integer type){
        return documentService.updateShowStatus(dto,type);
    }

    /**
     * 查询下一篇文档
     *
     * @return
     */
    @AutoLog(value = "文档表-查询下一篇文档")
    @ApiOperation(value = "文档表-查询下一篇文档", notes = "文档表-查询下一篇文档")
    @GetMapping("/getAfterDocument")
    public Result getAfterDocument(@RequestParam("documentId") String documentId) {
        DocumentVO document = documentService.getAfterDocument(Integer.valueOf(documentId));
        return Result.OK(document);
    }

    /**
     * 查询上一篇文档
     *
     * @return
     */
    @AutoLog(value = "文档表-查询上一篇文档")
    @ApiOperation(value = "文档表-查询上一篇文档", notes = "文档表-查询上一篇文档")
    @GetMapping("/getPreDocument")
    public Result getPreDocument(@RequestParam("documentId") String documentId) {
        DocumentVO document = documentService.getPreDocument(Integer.valueOf(documentId));
        return Result.OK(document);
    }
}
